package com.example.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.jsontest.R;
import com.example.model.Album;
import com.example.service.AlbumBindService;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class GridViewAdapter extends BaseAdapter {

	private List<Album> list = new ArrayList<Album>();
	Context context;
	private AlbumBindService mService;
	public GridViewAdapter(Context context, List<Album> list,AlbumBindService mService) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.list = list;
		this.mService = mService;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		if(this.list.size() > 0)
			return this.list.get(position);
		else
			return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stud
		ViewHolder holder;
		if (null == convertView) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.listitem, null);
			holder.tv = (TextView) convertView.findViewById(R.id.listitem_textview);
			holder.iv = (ImageView) convertView.findViewById(R.id.listitem_headerimage);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tv.setText(this.list.get(position).getTitle());
		if(list.get(position).getImg() != null) {
	        Bitmap bit = mService.getImage(list.get(position).getImg());
	        if(bit != null) {
	        	holder.iv.setImageBitmap(bit);
	        }
		}
		return convertView;
	}

	class ViewHolder {
		ImageView iv;
		TextView tv;
	}

}
